﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NReports.Data;

namespace NReports.Data {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    /// 
    public class DataSetCommand : IDbCommand {

        DataSetConnection _connection;
        string _command;
        DataParameterCollection _parameters = new DataParameterCollection();

        public DataSetCommand(DataSetConnection conn) {
            _connection = conn;
        }


        #region IDbCommand Members

        public void Cancel() {
            throw new NotImplementedException("Cancel not implemented");
        }

        public void Prepare() {
            return;
        }

        public CommandType CommandType {
            get {
                throw new NotImplementedException("CommandType not implemented");
            }
            set {
                throw new NotImplementedException("CommandType not implemented");
            }
        }

        public IDataReader ExecuteReader(System.Data.CommandBehavior behavior) {
            if (!(behavior == CommandBehavior.SingleResult ||
                  behavior == CommandBehavior.SchemaOnly))
                throw new ArgumentException("ExecuteReader supports SingleResult and SchemaOnly only.");

            var dataSet = _connection.DataSet;
            if (dataSet == null) throw new Exception("Invalid ConnectionString: " + _connection.ConnectionString);

            if (this.CommandText == "SCHEMA") {
                var table = new DataTable("TABLES");
                table.Columns.Add(new DataColumn("TABLE_NAME", typeof(string)));
                for (int i = 0; i < dataSet.Tables.Count; i++) {
                    var row = table.NewRow();
                    row["TABLE_NAME"] = dataSet.Tables[i].TableName;
                    table.Rows.Add(row);
                }
                return table.CreateDataReader();
            } else {
                for (int i = 0; i < dataSet.Tables.Count; i++) {
                    if (this.CommandText.ToLower().Contains("from " + dataSet.Tables[i].TableName.ToLower())) {
                        return dataSet.Tables[i].CreateDataReader();
                    }
                }
            }
            throw new Exception("Command not supported");
        }

        IDataReader System.Data.IDbCommand.ExecuteReader() {
            return ExecuteReader(System.Data.CommandBehavior.SingleResult);
        }

        public object ExecuteScalar() {
            throw new NotImplementedException("ExecuteScalar not implemented");
        }

        public int ExecuteNonQuery() {
            throw new NotImplementedException("ExecuteNonQuery not implemented");
        }

        public int CommandTimeout {
            get {
                return 0;
            }
            set {
                throw new NotImplementedException("CommandTimeout not implemented");
            }
        }

        public IDbDataParameter CreateParameter() {
            return new DataSetParameter();
        }

        public IDbConnection Connection {
            get {
                return this._connection;
            }
            set {
                throw new NotImplementedException("Setting Connection not implemented");
            }
        }

        public UpdateRowSource UpdatedRowSource {
            get {
                throw new NotImplementedException("UpdatedRowSource not implemented");
            }
            set {
                throw new NotImplementedException("UpdatedRowSource not implemented");
            }
        }

        public string CommandText {
            get {
                return this._command;
            }
            set {
                if (string.IsNullOrEmpty(value)) {
                    this._command = "SCHEMA";
                } else {
                    this._command = value;
                }

            }
        }

        public IDataParameterCollection Parameters {
            get {
                return _parameters;
            }
        }

        public IDbTransaction Transaction {
            get {
                throw new NotImplementedException("Transaction not implemented");
            }
            set {
                throw new NotImplementedException("Transaction not implemented");
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose() {
            // nothing to dispose of
        }

        #endregion
    }

}
